package com.example.jiaju.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.example.jiaju.R;
import com.example.jiaju.entity.Consult;
import com.example.jiaju.ui.ConsultFragment;
import com.example.jiaju.ui.msg.ChatActivity;

import java.util.List;

public class ConsultAdapter extends RecyclerView.Adapter<ConsultAdapter.MyViewHolder> {

    private List<Consult> consultList;
    private Context context;

    public ConsultAdapter(List<Consult> consultList,Context context) {
        this.consultList = consultList;
        this.context=context;
    }

    //     设置RecyclerView中的内容
    /*加载并返回布局文件   java->xml*/
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.consult_recyclerview_item,parent,false);
        MyViewHolder myViewHolder=new MyViewHolder(view);
        return myViewHolder;
    }

    /*填充，修改布局里的控件内容*/
    /*1.可以将数据提前填充到一个数组中
     * 2.通过数组的下标获取到值，填充到控件中*/
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

        //填充内容替换成数据接口请求
        Consult consult=consultList.get(position);
        holder.headImg.setImageResource(R.drawable.head1);
        holder.name.setText(consult.getName());
        holder.detail.setText(consult.getDetail());
        holder.consultRootView.setOnClickListener(v -> {
            Intent intent=new Intent(context, ChatActivity.class);
            intent.putExtra("consult",consult);
            context.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return consultList.size();
    }
    //    获取consult_item中控件
    static class MyViewHolder extends RecyclerView.ViewHolder{
        //命名对应consult实体类的变量名
        ImageView headImg;
        TextView name;
        TextView detail;
        ConstraintLayout consultRootView;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            headImg=itemView.findViewById(R.id.consult_header);
            name=itemView.findViewById(R.id.consult_name);
            detail=itemView.findViewById(R.id.consult_detail);
            consultRootView=itemView.findViewById(R.id.consult_rootView);
        }
    }
}
